/*
 * 
 *  This file is part of VADER.
 *
 *  VADER is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  VADER is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package vegadataeditor;

import org.jdesktop.application.Application;
import org.jdesktop.application.SingleFrameApplication;
import java.util.prefs.Preferences;
import java.sql.*;

import javax.swing.JOptionPane;
import vegadataeditor.dataSources.GleonWebServiceSource;
import vegadataeditor.dataSources.SourceConnectionException;


/**
 * The main class of the application.
 */
public class VegaDataEditorApp extends SingleFrameApplication {

    public static String dbUser;
    public static String dbPass;
    public static String dbHost;
    public static String dbName;
    
    public static Connection conn;
    
    
    /**
     * At startup create and show the main frame of the application.
     */
    @Override protected void startup() {
        
        /*

        //first, get previously stored dbSettings if they exist
        retrieveStoredSettings();
        
        DatabaseConnectionDialog settingsDialog = 
                new DatabaseConnectionDialog(null,true);
        settingsDialog.setUsername(dbUser);
        settingsDialog.setPassword(dbPass);
        settingsDialog.setDatabaseName(dbName);
        settingsDialog.setHost(dbHost);
        
        
        //Start with status ok to get into while loop
        int returnStatus = DatabaseConnectionDialog.RET_OK;
        
        //while they continue making attempts by clicking OK
        while(returnStatus == DatabaseConnectionDialog.RET_OK){
            show(settingsDialog);
            returnStatus = settingsDialog.getReturnStatus();
            if(returnStatus == DatabaseConnectionDialog.RET_CANCEL)
                return;
            
            dbUser = settingsDialog.getUsername();
            dbPass = settingsDialog.getPassword();
            dbName = settingsDialog.getDatabaseName();
            dbHost = settingsDialog.getHost();
            
            if(checkDBConnection()){
                saveSettings();
                break;
            }
                
        }
        */

        VegaDataEditorView app = new VegaDataEditorView(this);
        StartupWaitDialog2 wd = new StartupWaitDialog2();


        show(wd);

        wd.update(wd.getGraphics());


        try{
        GleonWebServiceSource g = new GleonWebServiceSource();

        app.setStreams(g.getAllStreams());
        app.setVariables(g.getVariables());
        app.setSites(g.getSites());

        }catch(SourceConnectionException sce){
            JOptionPane.showMessageDialog(null,"Fatal error. " + 
                    "Unable to connect to central database. " + 
                    "Please check your internet connection or " + 
                    "contact the GLEON IT group at gleon-software@gleonrcn.org");
        }

        wd.setVisible(false);
        wd.dispose();
        wd = null;


        show(app);

    }

    /**
     * This method is to initialize the specified window by injecting resources.
     * Windows shown in our application come fully initialized from the GUI
     * builder, so this additional configuration is not needed.
     * 
     * @param root window to initialize
     */
    @Override protected void configureWindow(java.awt.Window root) {
    }

    /**
     * A convenient static getter for the application instance.
     * @return the instance of VegaDataEditorApp
     */
    public static VegaDataEditorApp getApplication() {
        return Application.getInstance(VegaDataEditorApp.class);
    }

    /**
     * Main method launching the application.
     * @param args No arguments supported
     */
    public static void main(String[] args) {
        launch(VegaDataEditorApp.class, args);
    }
    
    /**
     * Checks the db connection info by attempting to connect to the database.
     *
     * @return false if unable to make a connection with current info. else true
     */
    public boolean checkDBConnection(){
        try{
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            conn = DriverManager.getConnection("jdbc:mysql://"+
                    dbHost+"/"+
                    dbName,
                    dbUser,
                    dbPass);
            
            
        }catch(java.lang.ClassNotFoundException cnf){
            System.out.println(cnf.toString());
        }catch(java.lang.InstantiationException ie){
            System.out.println(ie.toString());
        }catch(java.sql.SQLException se){
            System.out.println(se.toString());
            return false;
        }catch(Exception e){
            System.out.println(e.toString());
            return false;
        }
        
        return true;

    }
    
    /**
     * Retrieves the stored db conn settings
     */
    public void retrieveStoredSettings(){
        Preferences prefs = java.util.prefs.Preferences
                .userNodeForPackage(VegaDataEditorApp.class);
        
        dbUser = prefs.get("dbUser", "");
        dbPass = prefs.get("dbPass", "");
        dbHost = prefs.get("dbHost","localhost");
        dbName = prefs.get("dbName", "vega");
    }
    
    /**
     * Saves the database connection settings.
     */
    public void saveSettings(){
        Preferences prefs = java.util.prefs.Preferences
                .userNodeForPackage(VegaDataEditorApp.class);
        prefs.put("dbUser", dbUser);
        prefs.put("dbPass", dbPass);
        prefs.put("dbHost", dbHost);
        prefs.put("dbName", dbName);
        try{
            prefs.flush();
        }catch(java.util.prefs.BackingStoreException bse){
            System.out.println(bse.toString());
        }
    }
    
    


 
}
